<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 303-3014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace plugins\map\Controller; //Demo插件英文名，改成你的插件英文就行了
use Api\Controller\PluginController;//插件控制器基类



class AdminIndexController extends PluginController{

	
	
	function _initialize(){
		$adminid=sp_get_current_admin_id();//获取后台管理员id，可判断是否登录
		if(!empty($adminid)){
			$this->assign("adminid",$adminid);
		}else{
		
		}
	}
	
	function index(){
		$this->display();
	}
	
	
	function robot(){
		if($_POST){
			 @$fp=fopen("robots.txt","w");
			 fwrite($fp,$_POST['content']);
			 fclose($fp);
			 $this->success();
		}else{
			$lines=file("robots.txt");
			$this->assign('lines',$lines);
			$this->display();
		}
		
	}
	
	
	function sc_add(){
		
		$data=1;
		$this->assign('data',$data);
		$root='http://'.$_SERVER['HTTP_HOST'];
	
		for($i=1;$i<=100;$i++){
			unlink('./sitemap_'.$i.'.xml');
		}
		
		
		
		
		header("Content-Type: Text/Html;Charset=UTF-8");
    	//实例化Sitemap类
		require './plugins/Map/Controller/Sitemap.class.php';
		
		
		$Sitemap = new Sitemap(); 
		
		$config=M('plugins')->where("name='map'")->find();
		$cmf_settings = json_decode($config['config'],true);
		
		
		$num=$cmf_settings['num'];
	
		//list读取
		$lists_term=M('terms')->select();
		$lists_c=M('terms')->count();
		
		//page读取
		$where_page=array("post_type=2 and post_status=1");
		$page=M('posts')->where($where_page)->select();
		$page_c=M('posts')->where($where_page)->count();
		
		//article读取
		$where_article=array("status=1");
		$article=M('term_relationships')->where($where_article)->select();
		$article_c=M('term_relationships')->where($where_article)->count();
		
		
		$count=$article_c+$page_c+$lists_c;
		
		
		//超过限制新建文件
		if($count>$num){
			require './plugins/Map/Controller/Sitemapindex.class.php';
		
		
			$Sitemapindex = new Sitemapindex(); 
			$shuliang=ceil($count/$num);
			
			for($i=1;$i<=$shuliang;$i++){
				$Sitemapindex->AddItem('http://'.$_SERVER['HTTP_HOST'].'/sitemap_'.$i.'.xml');
			}
			$Sitemapindex->SaveToFile('sitemap.xml');
			
			$list_page_count=$page_c+$lists_c;
			$redundant=$num-$list_page_count;
			
			
			for($i=1;$i<=$shuliang;$i++){
				$Sitemap_n = new Sitemap(); 
				if($i==1){
					$Sitemap_n->AddItem('http://'.$_SERVER['HTTP_HOST'], 0,'always');
					//list生成
					foreach ($lists_term as $v){
						$url=leuu('portal/list/index',array('id'=>$v['term_id']));
						$Sitemap_n->AddItem($root.$url, 1,'always');
						
					}
					//page生成
					foreach ($page as $v){
						$url=leuu('portal/page/index',array('id'=>$v['id']));
						$Sitemap_n->AddItem($root.$url, 5,'weekly');
						
					}
					//article生成
					foreach ($article as $key=>$v){
						if($key<=$redundant){
							$url=leuu('portal/article/index',array('id'=>$v['tid']));
							$Sitemap_n->AddItem($root.$url, 5,'daily');
						
						}
					}
					$Sitemap_n->SaveToFile('sitemap_'.$i.'.xml');
				} else{
					 $min_key=($i-1)*$num;
					 $max_key=$i*$num;
					
					foreach ($article as $key=>$v){
						
						
						if($key>$min_key && $key<=$max_key ){
							$url=leuu('portal/article/index',array('id'=>$v['tid']));
							$Sitemap_n->AddItem($root.$url, 5,'daily');
							
						}
					}
					$Sitemap_n->SaveToFile('sitemap_'.$i.'.xml');
				} 
				
				
				
			}
			
			$this->display('index');
			
		}else{
			//首页生成
				$Sitemap->AddItem('http://'.$_SERVER['HTTP_HOST'], 0,'always');
			//list生成
			foreach ($lists_term as $v){
				$url=leuu('portal/list/index',array('id'=>$v['term_id']));
				$Sitemap->AddItem($root.$url, 1,'always');
			}
			//page生成
			foreach ($page as $v){
				$url=leuu('portal/page/index',array('id'=>$v['id']));
				$Sitemap->AddItem($root.$url, 5,'weekly');
			}
			//article生成
			foreach ($article as $v){
				$url=leuu('portal/article/index',array('id'=>$v['tid']));
				$Sitemap->AddItem($root.$url, 5,'daily');
			}
			
			//生成到网站根目录
			$Sitemap->SaveToFile('sitemap.xml');
			$this->display('index');
			
		}
	}
	
}
